package fhp.hlhj.giantfold.adapter

import android.content.Context
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import com.bumptech.glide.Glide
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.network.Urls

/**
 * Created by Administrator on 2018\2\9 0009.
 */
class MerchantGridAdp(private val context: Context,private val datas:List<String>,private val savePath:String) :BaseAdapter() {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var convertView=convertView
        if (convertView==null){
            convertView=LayoutInflater.from(context).inflate(R.layout.merchant_img_item,null)
        }
        val img = convertView?.findViewOften<ImageView>(R.id.img)
        Glide.with(context).load(Urls.BaseUrl+savePath+datas[position]).asBitmap().into(img)
        return convertView!!
    }

    override fun getItem(position: Int): Any {
        return datas.get(position)
    }

    override fun getItemId(position: Int): Long {
     return position.toLong()
    }

    override fun getCount(): Int {
        return datas.size
    }
    fun <T : View> View.findViewOften(viewId: Int): T {
        var viewHolder: SparseArray<View> = tag as? SparseArray<View> ?: SparseArray()
        tag = viewHolder
        var childView: View? = viewHolder.get(viewId)
        if (null == childView) {
            childView = findViewById(viewId)
            viewHolder.put(viewId, childView)
        }
        return childView as T
    }
}